//
//  SSGAFSimpleTool.m
//  Bravo
//
//  Created by 吴凡 on 2017/11/7.
//  Copyright © 2017年 SSG. All rights reserved.
//

#import "SSGAFSimpleTool.h"

// 请求失败超时时间
static const NSTimeInterval kLZBaseRequestTimeoutInterval = 10;
//static NSInteger kLZBaseRequestRertyCount = 3;

@implementation SSGAFSimpleTool

static AFHTTPSessionManager *_manager = nil;
static SSGAFSimpleTool *_toolManager = nil;
static dispatch_once_t _onceToken;

+ (NSString *)imageBaseURL {
    return RootUrlImage;
}

+ (NSString *)baseURL{
//    if (Networking_Mode == 1) {
//        return BASE_URL;
//    } else if(Networking_Mode == 0) {
//        return BASE_URL_Test;
//    }
    return Main_Token_Model_MANAGER.RootUrl;
}

+ (NSString *)h5BaseUrl{
    return @"";
}

+ (instancetype)sharedManager {
    dispatch_once(&_onceToken, ^{
        _toolManager = [[SSGAFSimpleTool alloc]init];
        
        _manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:Main_Token_Model_MANAGER.RootUrl]];
        _manager.securityPolicy.allowInvalidCertificates = YES;
        _manager.securityPolicy.validatesDomainName = NO;
        _manager.requestSerializer = [AFJSONRequestSerializer serializer];
        _manager.requestSerializer.timeoutInterval = kLZBaseRequestTimeoutInterval;
        _manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        [_manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [_manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [_manager.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"image/jpeg",@"text/plain", nil];

        
        [_manager.requestSerializer setValue:kUSER_INFO_MANAGER.token forHTTPHeaderField:@"Authorization"];
//        [_manager.requestSerializer setValue:@"v1.0" forHTTPHeaderField:@"Api-Version"];
        [_manager.requestSerializer setValue:kHEADERS_VALUE forHTTPHeaderField:kHEADERS_KEY];

    });
    return _toolManager;
}



-(void)GET:(NSString *)path params:(NSDictionary *)params
   success:(LHResponseSuccess)success fail:(LHResponseFail)fail{
    
    AFHTTPSessionManager *manager = _manager;
    [manager.requestSerializer setValue:kUSER_INFO_MANAGER.token forHTTPHeaderField:@"Authorization"];
//    [manager.requestSerializer setValue:@"v1.0" forHTTPHeaderField:@"Api-Version"];
    [manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
       //NSDictionary *dic = [SSGAFSimpleTool responseConfiguration:responseObject];

        success(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(task,error);
    }];
}


-(void)POST:(NSString *)path params:(NSDictionary *)params
    success:(LHResponseSuccess)success fail:(LHResponseFail)fail{
    NSLog(@"http path:%@",path);
    AFHTTPSessionManager *manager = _manager;
    [manager.requestSerializer setValue:kUSER_INFO_MANAGER.token forHTTPHeaderField:@"Authorization"];
//    [manager.requestSerializer setValue:@"v1.0" forHTTPHeaderField:@"Api-Version"];
    [manager POST:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
       //NSDictionary *dic = [SSGAFSimpleTool responseConfiguration:responseObject];
        
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(task,error);
    }];
}

-(void)PUT:(NSString *)path
    params:(NSDictionary *)params
   success:(LHResponseSuccess)success
      fail:(LHResponseFail)fail{
    
    AFHTTPSessionManager *manager = _manager;
     [manager.requestSerializer setValue:kUSER_INFO_MANAGER.token forHTTPHeaderField:@"Authorization"];
//    [manager.requestSerializer setValue:@"v1.0" forHTTPHeaderField:@"Api-Version"];
    [manager PUT:path parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(task,error);
    }];
    
}
-(void)DELETE:(NSString *)path
       params:(NSDictionary *)params
      success:(LHResponseSuccess)success
         fail:(LHResponseFail)fail {
    AFHTTPSessionManager *manager = _manager;
    [manager.requestSerializer setValue:kUSER_INFO_MANAGER.token forHTTPHeaderField:@"Authorization"];
//    [manager.requestSerializer setValue:@"v1.0" forHTTPHeaderField:@"Api-Version"];
    [manager DELETE:path parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(task,error);
    }];
}

#pragma mark - Private
+(NSDictionary*)responseConfiguration:(id)responseObject{
    
    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    return dic;
}

@end
